var appdata = getApp().globalData
var task = require('./task.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 获取状态栏和用户栏高度
    StatusBar: appdata.StatusBar,
    CustomBar: appdata.CustomBar,
    // 第一视角
    first_person: false,
    show_mask_form: false, // [每次进入加载数据过程中的]蒙层界面
    show_give_button: false, // [送他一个]按钮
    show_attend_activity_form: false, // [立即参加]页面
    show_activity_end_form: false, // [活动结束]页面
    // 活动设置
    settings: {},
    // 券设置
    tickets: [],
    // 是否携带 openid
    share_user_openid: 0 // 默认为空
  },
  /**
   * 用户点击左上角返回首页
   */
  back_to_index: function(e) {
    wx.reLaunch({
      url: '/pages/index/index',
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function(res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      //console.log(res.target)
    }
    return {
      title: this.data.settings.name,
      path: 'pages/activity/collectlikes/collectlikes?openid=' + this.data.current_user_openid,
      imageUrl: 'http://wx2.sinaimg.cn/mw690/b05ad9f5ly1g3rdfazddfj20ku0gok5h.jpg'
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this
    // 获取排行榜数据
    task.get_rank_data().then(res => {
      //console.log("排行榜数据：", res)
      that.setData({ rank:res.data})
    })
    // 判断用户进入场景
    if (options.openid) {
      //console.log("场景1：用户从转发卡片进入,openid:" + options.openid)
      this.data.share_user_openid = options.openid
    } else {
      //console.log("场景2：用户主动从小程序进入，未携带openid")
      // 时宝顺：一会修改值为 0 'oek8g5flz1ayfGOgTp4NHUMCa-Pw'   君兰：'oek8g5TnV_eJL2FZ8zFLghpyXzVY'
      this.data.share_user_openid = 0
    }
    // //console.log("openid:",this.data.share_user_openid)
    this.toggle_mask_form(true)
    this.run_at_onshow()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  //************************** 程序入口 **************************** */
  run_at_onshow: function() {
    let that = this
    // 1. 获取活动设置 & 卡券信息
    task.get_activity_settings().then(res => {
      let res_data = res.data
      let settings = res_data.settings
      let tickets = res_data.tickets
      let in_activity_time = task.at_activity_time(settings.start_time, settings.end_time)
      //console.log("当前是否活动开启 并且 在活动日期内：", settings.open && in_activity_time)

      // 如果活动开启 && 不在活动时间范围内
      if (!(settings.open && in_activity_time)) {
        wx.showModal({
          title: '活动已结束',
          content: '非常抱歉，活动已结束！下次记得早点来噢~',
          showCancel: false,
          confirmText: '我知道了',
          complete: function() {
            that.back_to_index()
          }
        })
        return 0
      }

      // 活动开启 && 在活动时间范围内

      this.setData({
        settings: settings,
        tickets: tickets
      })
      // 初始化 towerSwiper 轮播图
      this.towerSwiper();
      // 获取当前用户openid、当前用户活动信息、 转发用户活动信息
      return task.get_both_user_info(this.data.share_user_openid);

    }).then(res => {
      //console.log("获取当前用户和转发用户的活动信息：", res)
      if (res == 0) return 0
      let data = res.result
      // 查询失败
      if (data.result !== 'success') {

        that.internet_abnormal()
        return 0
      }
      // 查询成功
      that.setData({
        current_user_openid: data.current_user_openid,
        current_user_activity_info: data.current_user_activity_info,
        share_user_activity_info: data.share_user_activity_info,
      })
      that.update_exchange_button_status()
      // 判断场景
      if (this.data.share_user_openid == 0) {
        // 从主程序入口自己进入
        that.from_main_program()
      } else {
        //  从转发卡片进入
        that.from_share_card()
      }
    })
  },
  //************************** 从主程序入口自己进入 **************************** */
  // ------------------------------main
  from_main_program: function() {
    let current_user_activity_info = this.data.current_user_activity_info

    // a. 如果用户参加过活动
    if (current_user_activity_info) {
      // 第一视角：显示用户活动信息
      this.setData({
        first_person: true,
        current_user_activity_info: current_user_activity_info
      })
      this.toggle_mask_form(false)
      return
    }
    // b.用户未参加过活动
    this.toggle_attend_activity_form(true)
  },
  // ------------------------------点击 立即参加
  attend_activity: function(e) {
    let that = this;
    let result = e.detail.errMsg
    // 1. 用户拒绝授权
    if (result == 'getUserInfo:fail auth deny') {
      wx.showToast({
        title: '请先完成授权',
        icon: 'none'
      })
      return
    }
    // 2. 用户授权成功
    wx.showLoading({
      title: '正在为您报名...'
    })

    // 获取用户信息
    wx.pro.getUserInfo().then(res => {
        let userInfo = res.userInfo
        //console.log("用户信息：", userInfo)
        that.data.userInfo = userInfo // 将用户信息存储备用
        // 为用户执行登录操作
        return wx.cloud.callFunction({
          name: 'login'
        })
      }).then(res => {
        let login_res = res.result
        //console.log("用户执行登录结果：", res.result)
        if (login_res.login == 'fail') {
          // 显示网络异常
          that.internet_abnormal()
          return 0
        }
        // 用户登录成功
        //记录用户信息至内存
        wx.setStorageSync('userInfo', res.result) 
        let new_user_activity_info = {
          _id: that.data.current_user_openid,
          openid: that.data.current_user_openid,
          user_avatar_url: that.data.userInfo.avatarUrl,
          user_nick_name: that.data.userInfo.nickName,
          current_likes: 0,
          give_likes: 0,
          consume_likes: 0,
        }
        that.setData({
          current_user_activity_info: new_user_activity_info
        })
        return task.cloud_add_record('activity_users', new_user_activity_info)
      })
      .then(res => {
        //console.log("用户执行报名写入：", res)
        if (res == 0) return 0
        // 写入报名信息失败 
        if (res.errMsg !== "collection.add:ok") {
          that.internet_abnormal()
          return 0
        }
        // 写入报名信息成功
        wx.hideLoading()
        that.setData({
          first_person: true
        })
        that.toggle_attend_activity_form(false)
        that.toggle_mask_form(false)
      })
  },
  //**************************    共用功能函数 **************************** */
  update_exchange_button_status: function() {
    // let settings = this.data.settings
    // if (!settings) {
    //   return
    // }
    // settings.start_time = task.formatTime(settings.start_time).split(' ')[0]
    // settings.end_time = task.formatTime(settings.end_time).split(' ')[0]
    // this.setData({ settings: settings})
    ////console.log(task.formatTime(settings.start_time))
    //console.log("刷新显示按钮")
    let tickets = this.data.tickets
    let current_user_activity_info = this.data.current_user_activity_info
    // tickets 有数据
    if (!tickets) {
      //console.log("tickets未定义", tickets)
      return
    }
    if (!current_user_activity_info) {
      //console.log("current_user_activity_info未定义", current_user_activity_info)
      return
    }
    // 用户信息中是否有 exchanged_ticketid 定义，没有说明没有兑换过,无需刷新tickets属性
    if (!current_user_activity_info.exchanged_ticketid) {
      //console.log("兑换记录未定义", current_user_activity_info.exchanged_ticketid)
      return
    } 
    // //console.log(tickets, current_user_activity_info)

    let exchange_record = current_user_activity_info.exchanged_ticketid
    for (let i = 0; i < tickets.length; i++) {
      let id = Number(tickets[i].id)
      let times = 0
      for (let n = 0; n < exchange_record.length; n++) {
        if (exchange_record[n] == id) {
          times += 1
        }
      }
      tickets[i].current_user_exchange_count = times
    }
    this.setData({
      tickets: tickets
    })
  },
  //**************************    从转发卡片进入 **************************** */
  // ------------------------------main
  from_share_card: function() {
    let that = this
    let share_user_openid = that.data.share_user_openid
    let current_user_openid = that.data.current_user_openid

    // 1. 转发者 和 当前用户是同一人
    if (share_user_openid == current_user_openid) {
      // 设置：第一视角、显示信息
      that.setData({
        first_person: true,
        current_user_activity_info: that.data.share_user_activity_info
      })
      that.toggle_mask_form(false)

    } else {
      // 2. 转发者 和 当前用户是两个人
      that.share_user_is_not_current_user()
    }
  },
  // ------------------------------  情景2 (从卡片计入) ： a. 转发者 和 当前用户是两个人
  share_user_is_not_current_user: function() {
    let that = this
    wx.showLoading({
      title: '疯狂加载中...'
    })

    // ** 判断用户是否参加过活动 **
    let current_user_activity_info = that.data.current_user_activity_info

    if (current_user_activity_info == false) {

      // 1. 当前用户没有参加此活动

      wx.hideLoading()
      that.data.current_user_type = 'new' // 新用户
      that.setData({
        first_person: false
      }) // 第三视角
      that.toggle_give_button(true) // 显示赠送按钮
      that.toggle_mask_form(false) // 关闭遮罩

    } else {

      that.data.current_user_type = 'old'

      // 2. 当前用户已参加此活动

      let current_user_activity_info = that.data.current_user_activity_info
      let max_give = that.data.settings.give_likes_chances_per_person

      // ** 判断用户是否还有点赞机会 **
      // a. 没有点赞机会了
      if (current_user_activity_info.give_likes >= max_give) {
        wx.hideLoading()
        wx.showToast({
          title: '您没有粽子啦~',
          icon: 'none'
        })
        that.setData({
          first_person: false
        }) // 第三视角
        that.toggle_give_button(false) // 不显示赠送按钮
        that.toggle_mask_form(false) // 关闭遮罩
        return
      }
      // b. 还有点赞机会
      // ** 判断是否给相同用户点赞过 **
      task.already_give_like(that.data.current_user_openid, that.data.share_user_openid)
        .then(res => {
          wx.hideLoading()
          //console.log("当前用户是否为转发用户点赞过：", res)
          // a. 网络故障
          if (res.errMsg !== 'collection.count:ok') {
            that.internet_abnormal()
            return 0
          }
          // b. 已经点赞
          if (res.total >= this.data.settings.same_person_can_give) {
            wx.showToast({
              title: '不能重复赠送噢',
              icon: 'none'
            })
            that.setData({
              first_person: false
            }) // 第三视角
            that.toggle_give_button(false) // 不显示赠送按钮
            that.toggle_mask_form(false) // 关闭遮罩
            return 0
          }
          // c. 具备点赞资格
          that.setData({
            first_person: false
          }) // 第三视角
          that.toggle_give_button(true) // 显示赠送按钮
          that.toggle_mask_form(false) // 关闭遮罩
        })
    }
  },
  // ------------------------------  a. 转发者 和 当前用户是两个人 ： (1)  [新用户/老用户] 有点赞资格时执行
  give_like: function(e) {

    let that = this;
    let result = e.detail.errMsg
    let current_user_type = this.data.current_user_type
    wx.showLoading({
      title: '疯狂包粽子中...',
    })

    // ** 获得用户权限 ** 

    // a. 用户拒绝授权
    if (result == 'getUserInfo:fail auth deny') {
      wx.showToast({
        title: '授权后才能参加',
        icon: 'none'
      })
      wx.hideLoading()
      return
    }
    // b. 用户授权成功--存储用户信息
    wx.pro.getUserInfo().then(res => {
      let userInfo = res.userInfo
      //console.log("用户信息：", userInfo)
      that.data.userInfo = userInfo // 将用户信息存储备用

      // ** 判断是新用户还是老用户 **
      if (current_user_type == 'new') {
        // a. 新用户 执行操作
        that.new_user_actions()

      } else {
        // b. 老用户 执行操作
        that.old_user_actions()
      }
    })
  },
  // ------------------------------   [新用户] 点赞执行操作
  new_user_actions: function() {
    let that = this
    // ** 执行登录操作 ** 
    wx.cloud.callFunction({
        name: 'login'
      })
      .then(res => {
        let login_res = res.result
        //console.log("用户执行登录结果：", res.result)
        // a. 登录操作：网络异常
        if (login_res.login == 'fail') {
          that.internet_abnormal()
          wx.hideLoading()
          return 0
        }
        //记录用户信息至内存
        wx.setStorageSync('userInfo', res.result)

        // b. 登录成功：开始新建用户信息
        let new_user_activity_info = {
          _id: that.data.current_user_openid,
          openid: that.data.current_user_openid,
          user_avatar_url: that.data.userInfo.avatarUrl,
          user_nick_name: that.data.userInfo.nickName,
          current_likes: 0,
          give_likes: 0,
          consume_likes: 0,
        }
        return task.cloud_add_record('activity_users', new_user_activity_info)
      })
      .then(res => {
        if (res == 0) return 0
        //console.log("用户执行报名写入：", res)

        // ** 新用户信息写入 **

        // a. 新用户信息写入 失败
        if (res.errMsg !== "collection.add:ok") {
          that.internet_abnormal()
          wx.hideLoading()
          return 0
        }
        // b. 新用户信息写入 成功

        // ** 记录点赞操作 ** 执行云函数：给赞方 数值+1 /得赞方 数值 +1 / 点赞记录
        return task.record_activity_log(that.data.current_user_openid, that.data.share_user_openid)
      }).then(res => {
        if (res == 0) return 0
        //console.log("点赞记录：", res)
        wx.hideLoading()

        // a. 记录点赞操作：网络异常
        if (res.errMsg !== "cloud.callFunction:ok") {
          that.internet_abnormal()
          wx.hideLoading()
          return 0
        }
        // b. 记录点赞操作：失败
        let data = res.result
        if (data.result == 'fail') {
          that.internet_abnormal()
          wx.hideLoading()
          return 0
        }
        // c. 记录点赞操作：成功
        wx.hideLoading()
        that.toggle_mask_form(false)
        that.toggle_give_button(false)
        that.setData({
          first_person: false,
          share_user_activity_info: data.recv_user_info,
          current_user_activity_info: data.give_user_info,
        })
      })
  },
  // ------------------------------   [老用户] 点赞执行操作
  old_user_actions: function() {
    let that = this
    let current_user_activity_info = that.data.current_user_activity_info
    let userInfo = that.data.userInfo

    // ** 老用户个人信息更新 ** 
    task.update_user_userinfo(that.data.current_user_openid, userInfo)
      .then(res => {
        //console.log("老用户个人信息刷新：", res)
        // a. 老用户个人信息更新:网络异常/更新失败
        if (res.errMsg !== 'document.update:ok') {
          that.internet_abnormal()
          wx.hideLoading()
          return 0
        }
        // b. 个人信息更新成功

        // ** 记录点赞操作 ** 执行云函数：给赞方 数值+1 /得赞方 数值 +1 / 点赞记录
        return task.record_activity_log(that.data.current_user_openid, that.data.share_user_openid)

      }).then(res => {
        wx.hideLoading()
        if (res == 0) return
        //console.log("老用户记录点赞结果", res)
        let data = res.result
        // a. 记录点赞操作 : 失败
        if (res.errMsg !== "cloud.callFunction:ok" || data.result == 'fail') {
          that.internet_abnormal()
          return 0
        }
        // b. 记录点赞操作 : 成功
        that.toggle_give_button(false)
        that.toggle_mask_form(false)
        that.setData({
          first_person: false,
          share_user_activity_info: data.recv_user_info,
          current_user_activity_info: data.give_user_info,
        })
      })
  },
  // ------------------------------   用户点击 【我也要集赞】
  also_want_to_attend: function(e) {
    wx.showToast({
      title: '快转发集粽子吧',
      icon: 'none'
    })
    this.setData({
      first_person: true
    })
  },

  //**************************    兑换功能区域 **************************** */
  click_exchange: function(e) {
    //console.log("************************兑换功能测试*****************************")
    let that = this
    let ticketid = Number(e.currentTarget.dataset.id)
    let ticket_exchange_max = 0

    wx.showLoading({
      title: '疯狂加载中...',
    })

    //--------------------1. 判断是否还在活动时间内[获取活动设置 & 卡券信息]
    task.get_activity_settings()
      .then(res => {
        let res_data = res.data
        let settings = res_data.settings
        let tickets = res_data.tickets
        let in_activity_time = task.at_activity_time(settings.start_time, settings.end_time)
        //console.log("当前是否活动开启 并且 在活动日期内：", settings.open && in_activity_time)
        // 如果活动开启 && 不在活动时间范围内
        if (!(settings.open && in_activity_time)) {
          wx.hideLoading()
          wx.showModal({
            title: '活动已结束',
            content: '非常抱歉，活动已结束！下次记得早点来噢~',
            showCancel: false,
            confirmText: '我知道了',
            complete: function() {
              that.back_to_index()
            }
          })
          return 0
        }
        // 活动开启 && 在活动时间范围内: 更新卡券信息和设置
        this.setData({
          settings: settings,
          tickets: tickets
        })
        that.update_exchange_button_status()
        //--------------------2. 判断当前id对应的券是否还有库存 [从最新读取的信息中获取剩余卡券剩余数量]
        let ticket_rest = tickets[ticketid].total_count
        ticket_exchange_max = tickets[ticketid].exchange_count_per_user
        //console.log("卡券id： " + ticketid + " 剩余：" + ticket_rest + " 每人可兑换：" + ticket_exchange_max)
        if (ticket_rest == 0) {
          wx.hideLoading()
          wx.showModal({
            title: '您来慢了一步！',
            content: '好可惜，有个家伙抢先一步兑换走了此券',
            showCancel: false,
            confirmText: '我知道了',
          })
          return 0
        }
        //--------------------3. 判断该用户当前此券的领取数量
        return task.already_exchange_count(that.data.current_user_openid, ticketid)
      }).then(res => {
        if (res == 0) return
        //console.log("当前用户卡券[" + ticketid + "]兑换次数为：" + res.total + " 次", res)
        // a. 网络错误
        if (res.errMsg !== "collection.count:ok") {
          wx.hideLoading()
          that.internet_abnormal()
          return 0
        }
        // b. 用户兑换此券次数达到上限
        let already_exchange_count = res.total
        if (already_exchange_count >= ticket_exchange_max) {
          wx.hideLoading()
          wx.showModal({
            title: '已经兑换此礼品 ' + already_exchange_count + " 次",
            content: '快去兑换别的礼品吧！',
            showCancel: false,
            confirmText: '我知道了',
          })
          return 0
        }
        // b. 用户拥有兑换此券的资格

        //--------------------4. 判断当前用户是否完成绑卡操作
        return task.whether_have_vip_card(that.data.current_user_openid)
      }).then(res => {
        if (res == 0) return 0
        //console.log("当前用户的会员卡查询信息：", res)

        // a. 网络故障
        if (res.errMsg !== 'collection.get:ok' || res.data.length !== 1) {
          wx.hideLoading()
          wx.showToast({
            title: '网络繁忙...',
            icon: 'none'
          })
          return 0
        }

        // b. 查询信息无误

        if (res.data[0].cardid && res.data[0].cardid.length > 0) {
          //console.log('用户已开通小程序会员功能,cardid:', res.data[0].cardid)
          // 记录当前用户的 vip 卡号
          that.setData({
            current_user_cardid: res.data[0].cardid
          })
          that.perform_exchange_for_current_user(that.data.current_user_openid, ticketid)
          // 跳转至用户会员卡界面
          // util.openVipCard('p1Jdc1E9hvyb__Rw0xTd877hRWu8')
          // util.openVipCardByCode('p1Jdc1E9hvyb__Rw0xTd877hRWu8', res.data[0].cardid)
        } else {
          //console.log('用户尚未绑定会员卡')
          wx.hideLoading()
          wx.showModal({
            title: '未绑定会员卡',
            content: "积分券直接发放到您的会员卡中，请完成绑卡操作后再进行兑换",
            cancelText: '取消兑换',
            confirmText: '开卡指南',
            complete: function(res) {
              if (res.confirm) {
                //跳转至开通页面
                wx.navigateTo({
                  url: '/pages/openMembership/openMembership',
                })
              }
            }
          })
          return 0
        }
      })
  },
  // --------------------------------------------- 为当前用户执行兑换操作
  perform_exchange_for_current_user: function(openid, ticketid) {

    let that = this
    //  1.执行兑换时数据库相关操作：a.刷新该券库存 b.更新用户点赞数及 用户核销记录 c.兑换记录
    wx.cloud.callFunction({
      name: 'writeWhenExchange',
      data: {
        openid: openid,
        ticketid: ticketid
      }
    }).then(res => {
      //console.log('用户积分兑换数据库记录：', res)
      // a. 数据库写入操作失败
      if (res.errMsg !== 'cloud.callFunction:ok' || res.result.result !== 'success') {
        wx.hideLoading()
        that.internet_abnormal()
        return 0
      }
      // b. 数据库写入成功 -->同步数据显示
      that.data.new_data_after_exchange = res.result
      // that.setData({
      //   current_user_activity_info: that.data.new_data_after_exchange.current_user_activity_info,
      //   tickets: that.data.new_data_after_exchange.tickets
      // })
      let index = Number(ticketid)
      let add_points = res.result.tickets[index].value
      return wx.cloud.callFunction({
        name: 'pointsPay',
        data: {
          openid: openid,
          totalFee: -Number(add_points),
          template_cardid: 'p1Jdc1E9hvyb__Rw0xTd877hRWu8'
        }
      })
    }).then(res => {
      if (res == 0) return
      //console.log('用户积分发放结果：', res)
      // a. 积分发放失败
      if (res.errMsg !== 'cloud.callFunction:ok' || res.result.result !== 'success') {
        wx.hideLoading()
        that.internet_abnormal()
        return 0
      }
      // b.积分发放成功
      // 同步数据显示
      that.setData({
        current_user_activity_info: that.data.new_data_after_exchange.current_user_activity_info,
        tickets: that.data.new_data_after_exchange.tickets
      })
      that.update_exchange_button_status()

      let index = Number(ticketid)
      let points = that.data.new_data_after_exchange.tickets[index].value
      let vipcard = String(that.data.current_user_cardid)
      wx.hideLoading()
      wx.showModal({
        title: '兑换成功',
        content: '已将 ' + points + ' 积分发放到您的账户',
        confirmText: '立即查看',
        complete: function(res) {
          if (res.confirm) {
            //console.log(vipcard)
            task.openVipCardByCode('p1Jdc1E9hvyb__Rw0xTd877hRWu8', vipcard)
          }
        }
      })

    })
  },
  //**************************    蒙层显示控制 true: 打开 false: 关闭 **************************** */
  toggle_mask_form: function(bool) {
    this.setData({
      show_mask_form: bool
    })
  },
  toggle_attend_activity_form: function(bool) {
    this.setData({
      show_attend_activity_form: bool
    })
  },
  toggle_activity_end_form: function(bool) {
    this.setData({
      show_activity_end_form: bool
    })
  },
  toggle_give_button: function(bool) {
    this.setData({
      show_give_button: bool
    })
  },
  internet_abnormal: function() {
    let that = this
    wx.showModal({
      title: '网络异常',
      content: '请检查您的网络状态',
      confirmText: '稍后再试',
      showCancel: false,
      complete: function(res) {
        if (res.confirm) {
          that.back_to_index()
        }
      }
    })
  },
  temp: function() {
    //console.log("就不让你点进去")
  },
  //****************************************************** */
  //------- 查看赠送数据
  check_friends_which_give_me:function(){
    wx.navigateTo({
      url: "../checkMore/checkMore?type=recv&openid=" + this.data.current_user_openid,
    })
  },
  check_I_give_who: function () {
    wx.navigateTo({
      url: "../checkMore/checkMore?type=give&openid=" + this.data.current_user_openid,
    })
  },
  //------- 查看完整榜单
  check_total_rank:function(){
    wx.navigateTo({
      url: "../activityData/activityData?openid=" + this.data.current_user_openid,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    wx.stopPullDownRefresh();
    let that = this
    wx.showLoading({
      title: '拼命加载中...',
    })
    task.get_both_user_info(this.data.share_user_openid)
      .then(res => {
        //console.log("获取当前用户和转发用户的活动信息：", res)
        let data = res.result
        // 查询失败
        if (data.result !== 'success') {
          wx.hideLoading()
          that.internet_abnormal()
          return 0
        }
        // 查询成功
        that.setData({
          current_user_openid: data.current_user_openid,
          current_user_activity_info: data.current_user_activity_info,
          share_user_activity_info: data.share_user_activity_info,
        })
        that.update_exchange_button_status()
        wx.hideLoading()
      })
    // 获取排行榜数据
    task.get_rank_data().then(res => {
      //console.log("排行榜数据：", res)
      that.setData({ rank: res.data })
      wx.stopPullDownRefresh();
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  //------------------------------------------------------- 图片滑动相关程序
  // towerSwiper
  // 初始化towerSwiper
  towerSwiper(name) {
    let list = this.data.settings.display_images;
    for (let i = 0; i < list.length; i++) {
      list[i].zIndex = parseInt(list.length / 2) + 1 - Math.abs(i - parseInt(list.length / 2))
      list[i].mLeft = i - parseInt(list.length / 2)
    }
    this.setData({
      towerList: list
    })
  },
  // towerSwiper触摸开始
  towerStart(e) {
    this.setData({
      towerStart: e.touches[0].pageX
    })
  },

  // towerSwiper计算方向
  towerMove(e) {
    this.setData({
      direction: e.touches[0].pageX - this.data.towerStart > 0 ? 'right' : 'left'
    })
  },

  // towerSwiper计算滚动
  towerEnd(e) {
    let direction = this.data.direction;
    let list = this.data.towerList;
    if (direction == 'right') {
      let mLeft = list[0].mLeft;
      let zIndex = list[0].zIndex;
      for (let i = 1; i < list.length; i++) {
        list[i - 1].mLeft = list[i].mLeft
        list[i - 1].zIndex = list[i].zIndex
      }
      list[list.length - 1].mLeft = mLeft;
      list[list.length - 1].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    } else {
      let mLeft = list[list.length - 1].mLeft;
      let zIndex = list[list.length - 1].zIndex;
      for (let i = list.length - 1; i > 0; i--) {
        list[i].mLeft = list[i - 1].mLeft
        list[i].zIndex = list[i - 1].zIndex
      }
      list[0].mLeft = mLeft;
      list[0].zIndex = zIndex;
      this.setData({
        towerList: list
      })
    }
  },
})